package com.andertrift.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.jws.HandlerChain;
import javax.servlet.http.HttpServletRequest;

import model.Page;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import twitter4j.TwitterException;

import com.andertrift.connectors.FacebookConnector;
import com.andertrift.connectors.LingWareHouseConnector;
import com.andertrift.connectors.TwitterConnector;

import facebook4j.FacebookException;

@Controller
@RequestMapping("/send")
public class UpdateController {
	TwitterConnector twit;
	FacebookConnector conn;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView printFacebookform() {
		twit = new TwitterConnector();
		conn = new FacebookConnector();
		return new ModelAndView("send");
	}

	@RequestMapping(method = RequestMethod.POST)
	public String pushMessage(@RequestParam String text)
			throws FacebookException, TwitterException {
		conn.postMessage(text);
		twit.postMessage(text);
		return "send";
	}

	@RequestMapping(value = "/facebook", method = RequestMethod.GET)
	public @ResponseBody
	String facebookVerification(HttpServletRequest svr)
			throws FacebookException, TwitterException {
		String test = (String) svr.getParameter("hub.mode");
		return svr.getParameter("hub.challenge");
	}

	@RequestMapping(value = "/facebook", method = RequestMethod.POST)
	public void facebookReceiver(HttpServletRequest req)
			throws FacebookException {
		try {
			InputStream body = req.getInputStream();
			ObjectMapper mapper = new ObjectMapper();
			JsonNode rootNode = mapper.readTree(new InputStreamReader(body));
			JsonNode entry = rootNode.findValue("entry");
			StringBuilder sb = new StringBuilder();
			LingWareHouseConnector lwh = new LingWareHouseConnector();
			conn = new FacebookConnector();
			String id = null;
			String message = null;
			if (entry.findValue("verb").getTextValue().contentEquals("add")) {
				JsonNode item = entry.findValue("item");
				if (item.getTextValue().contentEquals("post") || item.getTextValue().contentEquals("status")) {
					id = entry.findValue("post_id").asText();
					message = conn.getPost(id);
					conn.postComment(id, lwh.parseSentence(message));

				} else if (item.getTextValue().contentEquals("comment")) {
					String message_id = entry.findValue("comment_id").asText();
					message = conn.getMessage(message_id);
					id = entry.findValue("parent_id").asText();
					conn.postComment(id, lwh.parseSentence(message));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}